package com.jsy.common.util.publicApi.mybatis;


import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ApiKeyMapper {

    /**
     * 插入API Key
     */
    @Insert("INSERT INTO api_keys (access_key, secret_key_hash, app_name, description, status, expired_at) " +
            "VALUES (#{accessKey}, #{secretKeyHash}, #{appName}, #{description}, #{status}, #{expiredAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insert(ApiKey apiKey);

    /**
     * 根据ID删除API Key
     */
    @Delete("DELETE FROM api_keys WHERE id = #{id}")
    int deleteById(Long id);

    /**
     * 更新API Key
     */
    @Update("UPDATE api_keys " +
            "SET access_key = #{accessKey}, " +
            "    secret_key_hash = #{secretKeyHash}, " +
            "    app_name = #{appName}, " +
            "    description = #{description}, " +
            "    status = #{status}, " +
            "    expired_at = #{expiredAt} " +
            "WHERE id = #{id}")
    int update(ApiKey apiKey);

    /**
     * 根据ID查询API Key
     */
    @Select("SELECT * FROM api_keys WHERE id = #{id}")
    ApiKey selectById(Long id);

    /**
     * 根据Access Key查询API Key
     */
    @Select("SELECT * FROM api_keys WHERE access_key = #{accessKey}")
    ApiKey selectByAccessKey(String accessKey);

    /**
     * 查询所有API Keys
     */
    @Select("SELECT * FROM api_keys")
    List<ApiKey> selectAll();

    /**
     * 根据状态查询API Keys
     */
    @Select("SELECT * FROM api_keys WHERE status = #{status}")
    List<ApiKey> selectByStatus(Integer status);
}